package com.nf.controller.login;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.UserInfoDao;
import com.nf.dao.impl.UserInfoDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 编写servle，实现用户注册
 */
@WebServlet("/api/UserRegisterServlet.do")
public class UserRegisterServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("username");
        String pass=req.getParameter("password");
        String phone=req.getParameter("phone");

        UserInfoDao dao=new UserInfoDaoImpl();
        int r=dao.add(name,pass,phone);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if(r==1){
            out.print(new Gson().toJson(successJson("注册成功，请进行登录")));
        }else {
            out.print(new Gson().toJson(errorJson("注册失败")));
        }

        out.flush();
        out.close();
    }
}
